package com.lanju.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.lanju.dao.BillsDao;
import com.lanju.dao.UserDao;
import com.lanju.entity.Bills;
import com.lanju.entity.User;
import com.lanju.entity.UserIdCard;
import com.lanju.service.BillsService;


@Service
public class BillsServiceImpl implements BillsService{

    private static final Logger LOGGER = LoggerFactory.getLogger(BillsServiceImpl.class);
    
    @Autowired
    BillsDao billsDao;
    @Autowired
    UserDao userDao;
    
	@Override
	@Transactional(value="financialmarketingTM", readOnly=true)
	public Map<String, Object> deleteBills(Map<String, Object> param) {
		Map<String, Object> result = new HashMap<String, Object>();
		
		try {
			billsDao.deleteBills(param);
			result.put("success", true);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			result.put("success", false);
		}
		return result;
	}

	@Override
	@Transactional(value="financialmarketingTM", readOnly=true)
	public Map<String, Object> queryDetailById(Map<String, Object> param) {
		Map<String, Object> rsMap = new HashMap<String, Object>();
		List<Map> list = billsDao.queryDetailById(param);
		rsMap.put("row", list);
		return rsMap;
	}

//	@Override
//	@Transactional(value="financialmarketingTM", readOnly=true)
//	public Map<String, Object> updateBills(String url,String id) {
//		Map<String, Object> result = new HashMap<String, Object>();
//		return billsDao.updateBills(url,id);
//	}

	@Override
	@Transactional(value="financialmarketingTM", readOnly=true)
	public List<Map> queryBillByUserId(Map<String, Object> param) {
		
		return billsDao.queryBillByUserId(param);
	}

	 @Override
	    @Transactional(value="financialmarketingTM", readOnly=true)
		public Map<String, Object> queryBillsList(Map<String, Object> param) {
	    	List<Bills> list = billsDao.queryBillsList(param);
	        long count = billsDao.countBillsList(param);
	        Map<String, Object> map = new HashMap<String, Object>();
	        map.put("total", count);
	        map.put("rows", list);
	        return map;
		}

	@Override
	@Transactional(value="financialmarketingTM", rollbackFor = Exception.class)
	public Map<String, Object> addBills(Map<String, Object> param) {
		Map<String, Object> result=new HashMap<>(); 
		try {
			Bills bills=new Bills();
			bills.setCreateTime(new Date());
			bills.setBillsId(UUID.randomUUID().toString());
			bills.setBillType("2");
			bills.setUserType(param.get("userType").toString());
			bills.setImgUrl(param.get("imgurl").toString());
			bills.setUserId(param.get("userId").toString());
			bills.setHouseId( param.get("houseId").toString());
			bills.setBill_name(param.get("bill_name").toString());
			User user=userDao.getById(param.get("userId").toString());
			bills.setUser(user);
			billsDao.addBills(bills);
			result.put("success", true);
		} catch (Exception e) {
			result.put("success", false);
			e.printStackTrace();
		}
		return result;
	}

	@Override
	@Transactional(value="financialmarketingTM", rollbackFor = Exception.class)
	public Map<String, Object> updateBills(Map<String, Object> param) {
		Map<String, Object> result=new HashMap<>(); 
		try {
			Bills bills=billsDao.findById(param.get("billsId").toString());
			if(bills!=null){
				if(param.get("imgurl")!=null){
					bills.setImgUrl(param.get("imgurl").toString());	
				}
				bills.setHouseId( param.get("houseId").toString());
				bills.setBill_name(param.get("bill_name").toString());
				billsDao.merge2(bills);
				result.put("success",true);
			}
		} catch (Exception e) {
			result.put("success",false);
			e.printStackTrace();
		}
		return result;
	}

	@Override
	@Transactional(value="financialmarketingTM", rollbackFor = Exception.class)
	public Map<String, Object> addIdCard(Map<String, Object> param) {
		Map<String, Object> result=new HashMap<>(); 
		try {
			UserIdCard idcard=new UserIdCard();
			idcard.setImgId(UUID.randomUUID().toString());
			idcard.setImgUrl(param.get("zhengmianurl").toString());
			User user=userDao.getById(param.get("userId").toString());
			idcard.setUser(user);
			billsDao.addIdCard(idcard);
			
			UserIdCard idcard1=new UserIdCard();
			idcard1.setImgId(UUID.randomUUID().toString());
			idcard1.setImgUrl(param.get("fanmianurl").toString());
			idcard1.setUser(user);
			billsDao.addIdCard(idcard1);
			result.put("success", true);
		} catch (Exception e) {
			e.printStackTrace();
			result.put("success", false);
		}
		return result;
	}

	@Override
	public Map<String, Object> getIdCard(Map<String, Object> param) {
		Map<String, Object> result=new HashMap<>();
	try {
		User user=	userDao.getById(param.get("userId").toString());
		result.put("rows", user.getIdCardset());
	} catch (Exception e) {
		result.put("rows", null);
		e.printStackTrace();
	}
		return result;
	}
}
